<template>
  <common-layout>
    <div class="login-container">
      <div class="login-img">
        <img src="../../assets/img/login.png" alt="" />
      </div>
      <div class="login">
        <h2 class="title">{{ systemName }}</h2>
        <a-form @submit="onSubmit" :form="form">
          <div>
            <a-alert
              type="error"
              :closable="true"
              v-if="error"
              :message="error"
              @close="onClose"
              showIcon
              style="margin-bottom: 24px;"
            />
            <a-form-item>
              <a-input
                autocomplete="autocomplete"
                size="large"
                placeholder="starlink"
                v-decorator="[
                  'name',
                  {
                    rules: [
                      {
                        required: true,
                        message: '请输入账户名',
                        whitespace: true,
                      },
                    ],
                  },
                ]"
              >
                <a-icon slot="prefix" type="user" />
              </a-input>
            </a-form-item>
            <a-form-item>
              <a-input
                size="large"
                placeholder="123456"
                autocomplete="autocomplete"
                type="password"
                v-decorator="[
                  'password',
                  {
                    rules: [
                      {
                        required: true,
                        message: '请输入密码',
                        whitespace: true,
                      },
                    ],
                  },
                ]"
              >
                <a-icon slot="prefix" type="lock" />
              </a-input>
            </a-form-item>
          </div>
          <!-- <div>
            <a-checkbox :checked="true">自动登录</a-checkbox>
            <a style="float: right">忘记密码</a>
          </div> -->
          <a-form-item>
            <a-button
              :loading="logging"
              style="width: 100%;margin-top: 24px"
              size="large"
              htmlType="submit"
              type="primary"
              >登录</a-button
            >
          </a-form-item>
        </a-form>
      </div>
    </div>
  </common-layout>
</template>

<script>
import CommonLayout from '@/layouts/CommonLayout';
import { starLogin, getUserInfo } from '@/services/user';
import { setAuthorization } from '@/utils/request';
// import { loadRoutes } from '@/utils/routerUtil';
import { mapMutations } from 'vuex';

export default {
  name: 'Login',
  components: { CommonLayout },
  data() {
    return {
      logging: false,
      error: '',
      form: this.$form.createForm(this),
    };
  },
  computed: {
    systemName() {
      return this.$store.state.setting.systemName;
    },
  },
  methods: {
    ...mapMutations('account', [
      'setUser',
      'setPermissions',
      'setRoles',
      'setUsertObj',
    ]),
    onSubmit(e) {
      e.preventDefault();
      this.form.validateFields((err) => {
        if (!err) {
          this.logging = true;
          const name = this.form.getFieldValue('name');
          const password = this.form.getFieldValue('password');
          starLogin(name, password).then(this.afterLogin);
        }
      });
    },
    afterLogin(res) {
      this.logging = false;
      const loginRes = res.data;
      if (loginRes.code === 200) {
        const { user, permissions, roles } = {
          user: {
            name: 'ADMIN',
            avatar: 'AVATAR',
            address: 'CITY',
            position: 'POSITION',
          },
          permissions: [{ id: 'queryForm', operation: ['add', 'edit'] }],
          roles: [{ id: 'admin', operation: ['add', 'edit', 'delete'] }],
        };

        this.setUser(user);
        this.setPermissions(permissions);
        this.setRoles(roles);
        setAuthorization({
          token: loginRes.result.token,
          expireAt: new Date().getTime(),
        });
        // 获取路由配置
        // getRoutesConfig().then((result) => {
        //   const routesConfig = result.data.data;
        //   loadRoutes(routesConfig);
        // });
        getUserInfo(this.form.getFieldValue('name')).then((res) => {
          let obj = {
            ...res.data.result,
          };
          this.setUsertObj(obj);
          this.$router.push('/homePage');
          this.$message.success('登录成功', 3);
        });
      } else {
        this.error = loginRes.err_message;
        this.$message.info('测试账号:starlink 密码:123456', 3);
      }
    },
    onClose() {
      this.error = false;
    },
  },
};
</script>

<style lang="less" scoped>
.common-layout {
  .top {
    text-align: center;
    .header {
      height: 44px;
      line-height: 44px;
      a {
        text-decoration: none;
      }
      .logo {
        height: 44px;
        vertical-align: top;
        margin-right: 16px;
      }
    }
    .desc {
      font-size: 14px;
      color: @text-color-second;
      margin-top: 12px;
      margin-bottom: 40px;
    }
  }
  .login-container {
    width: 1000px;
    height: 500px;
    margin: 0 auto;
    background: #fff;
    border-radius: 30px;
    overflow: hidden;
    box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.2);
    .title {
      font-size: 30px;
      color: @title-color;
      font-family: 'Myriad Pro', 'Helvetica Neue', Arial, Helvetica, sans-serif;
      font-weight: normal;
      text-align: center;
      padding-top: 80px;
      padding-bottom: 20px;
    }
    .login-img {
      width: 500px;
      height: 500px;
      vertical-align: top;
      display: inline-block;
      img {
        display: block;
        width: 100%;
        height: 100%;
      }
    }
  }
  .login {
    width: 500px;
    height: 500px;
    padding: 20px 70px;
    box-sizing: border-box;
    vertical-align: top;
    display: inline-block;
    @media screen and (max-width: 576px) {
      width: 95%;
    }
    @media screen and (max-width: 320px) {
      .captcha-button {
        font-size: 14px;
      }
    }
    .icon {
      font-size: 24px;
      color: @text-color-second;
      margin-left: 16px;
      vertical-align: middle;
      cursor: pointer;
      transition: color 0.3s;

      &:hover {
        color: @primary-color;
      }
    }
  }
}
</style>
